// pages/main/admin-apps/meeting-application/meeting-application.js
Page({
  data: {
    formData: {
      title: '会议申请流程-庄嘉豪-2025-10-21',
      number: '',
      subject: '',
      startDate: '2025-10-21',
      startTime: '09:00',
      endDate: '2025-10-21',
      endTime: '17:00',
      location: '',
      attendees: '庄嘉豪',
      department: '信息技术部',
      convener: '庄嘉豪',
      reporter: '',
      signature: '',
      approvalDepartment: '信息技术部',
      approvalDate: '2025-10-21',
      agendaList: [
        {
          checked: false,
          topic: '',
          attachment: '',
          reporter: ''
        }
      ]
    }
  },

  onLoad() {
    // 页面加载时的初始化
    this.initFormData();
  },

  // 初始化表单数据
  initFormData() {
    const today = new Date();
    const dateStr = today.getFullYear() + '-' + 
                   String(today.getMonth() + 1).padStart(2, '0') + '-' + 
                   String(today.getDate()).padStart(2, '0');
    
    this.setData({
      'formData.startDate': dateStr,
      'formData.endDate': dateStr,
      'formData.approvalDate': dateStr
    });
  },

  // 输入框变化
  onInputChange(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    
    this.setData({
      [`formData.${field}`]: value
    });
  },

  // 开始日期变化
  onStartDateChange(e) {
    this.setData({
      'formData.startDate': e.detail.value
    });
  },

  // 开始时间变化
  onStartTimeChange(e) {
    this.setData({
      'formData.startTime': e.detail.value
    });
  },

  // 结束日期变化
  onEndDateChange(e) {
    this.setData({
      'formData.endDate': e.detail.value
    });
  },

  // 结束时间变化
  onEndTimeChange(e) {
    this.setData({
      'formData.endTime': e.detail.value
    });
  },

  // 添加议程
  onAddAgenda() {
    const agendaList = [...this.data.formData.agendaList];
    agendaList.push({
      checked: false,
      topic: '',
      attachment: '',
      reporter: ''
    });
    
    this.setData({
      'formData.agendaList': agendaList
    });
  },

  // 删除议程
  onRemoveAgenda() {
    const agendaList = [...this.data.formData.agendaList];
    if (agendaList.length > 1) {
      agendaList.pop();
      this.setData({
        'formData.agendaList': agendaList
      });
    } else {
      wx.showToast({
        title: '至少保留一个议程',
        icon: 'none'
      });
    }
  },

  // 议程复选框变化
  onAgendaCheckChange(e) {
    const index = e.currentTarget.dataset.index;
    const checked = e.detail.value;
    const agendaList = [...this.data.formData.agendaList];
    
    agendaList[index].checked = checked;
    this.setData({
      'formData.agendaList': agendaList
    });
  },

  // 议程输入变化
  onAgendaInputChange(e) {
    const index = e.currentTarget.dataset.index;
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    const agendaList = [...this.data.formData.agendaList];
    
    agendaList[index][field] = value;
    this.setData({
      'formData.agendaList': agendaList
    });
  },

  // 表单验证
  validateForm() {
    const { formData } = this.data;
    const requiredFields = [
      { field: 'subject', name: '会议主题' },
      { field: 'location', name: '会议地点' }
    ];
    
    for (let item of requiredFields) {
      if (!formData[item.field] || formData[item.field].trim() === '') {
        wx.showToast({
          title: `请填写${item.name}`,
          icon: 'none'
        });
        return false;
      }
    }
    
    return true;
  },

  // 审批日期变化
  onApprovalDateChange(e) {
    this.setData({
      'formData.approvalDate': e.detail.value
    });
  },

  // Tab 切换
  onTabChange(e) {
    console.log('Tab切换:', e.detail.activeKey);
    // 这里可以根据 activeKey 切换显示不同的内容，目前暂时只做了表单部分
  },

  // 保存草稿
  onSaveDraft() {
    console.log('保存草稿:', this.data.formData);
    wx.showToast({
      title: '草稿已保存',
      icon: 'success'
    });
  },

  // 提交表单
  onSubmit() {
    if (!this.validateForm()) {
      return;
    }
    
    console.log('提交表单数据:', this.data.formData);
    
    wx.showLoading({
      title: '提交中...'
    });
    
    // 这里可以调用API提交数据
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '提交成功',
        icon: 'success'
      });
      
      // 延迟返回上一页
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }, 2000);
  }
});

